package com.richest.api.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.richest.api.dao.ExtensionDetailMapper;
import com.richest.api.dao.UserExtensionMapper;
import com.richest.api.service.IUserExtensionService;
import com.richest.bean.ExtensionDetailBean;
import com.richest.bean.UserExtensionBean;
import com.richest.util.CallResults;

/**
 * 
 * @author zhaohongchao 2019年3月17日 下午12:52:56
 * @email 70375739@qq.com
 */
@Service
public class UserExtensionServiceImpl implements IUserExtensionService {

	@Autowired
	private UserExtensionMapper UserExtensionMapper;

	@Autowired
	private ExtensionDetailMapper extensionDetailMapper;

	@Override
	public UserExtensionBean selectById(Long lUserExtensionId) {
		return UserExtensionMapper.selectById(lUserExtensionId);
	}

	@Override
	public void insert(UserExtensionBean userExtensionBean) {
		UserExtensionMapper.insert(userExtensionBean);
	}

	@Override
	public void update(UserExtensionBean userExtensionBean) {
		UserExtensionMapper.update(userExtensionBean);
	}

	@Override
	public CallResults receiveExtension(Long userId, Long extensionDetailId) {
		ExtensionDetailBean extensionDetailBean = extensionDetailMapper.selectById(extensionDetailId);
		if (null == extensionDetailBean) {
			return CallResults.error("优惠信息不存在");
		}

		UserExtensionBean userExtensionBean = new UserExtensionBean();
		userExtensionBean.setAdmart_id(extensionDetailBean.getAdmart_id());
		userExtensionBean.setBegin_time(extensionDetailBean.getBegin_time());
		userExtensionBean.setCategory_id(extensionDetailBean.getCategory_id());
		userExtensionBean.setCreate_by(String.valueOf(userId));
		userExtensionBean.setCreate_time(new Date());
		userExtensionBean.setEnd_time(extensionDetailBean.getEnd_time());
		userExtensionBean.setExtension_detail_id(String.valueOf(extensionDetailId));
		userExtensionBean.setImage(extensionDetailBean.getImage());
		userExtensionBean.setLink(extensionDetailBean.getLink());
		userExtensionBean.setMoney(extensionDetailBean.getMoney());
		userExtensionBean.setName(extensionDetailBean.getName());
		userExtensionBean.setParams(extensionDetailBean.getParams());
		userExtensionBean.setStatus(1);
		userExtensionBean.setType(extensionDetailBean.getType());
		userExtensionBean.setUser_id(userId);
		UserExtensionMapper.insert(userExtensionBean);

		return CallResults.ok().put("data", userExtensionBean);
	}

	@Override
	public CallResults getExtensionList(Long userId, Integer type) {
		return CallResults.ok().put("data", extensionDetailMapper.getExtensionList(userId, type));
	}

	@Override
	public CallResults getOwnExtensionList(Long userId) {
		return CallResults.ok().put("data", UserExtensionMapper.getOwnExtensionList(userId));
	}

}
